#@package BasicTools
# This implements the basic paint tools.

from Interfaces import *
from PyQt4.QtCore import QObject, Qt, QLine
from PyQt4.QtGui import QColor, QFontMetrics, QBrush
from random import random

##basic tools plugin

charList=["A", "B", "C", 'D', 'E', 'F', 'G', 'H', 
          'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 
          'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
          'Y', 'Z']

class BasicToolsPlugin(QObject, BrushInterface, ShapeInterface, FilterInterface):
   
    def brushes(self):
        return ["Pencil", "Air Brush", "Random Letters", "Rectangle"]
    #brushes
    
    def mousePress(self, brush, painter, pos):
        self.imageList=[]
        self.pen=painter.pen()
        self.posList=[]
        self.brush=brush
        return self.mouseMove(brush, painter, pos, pos)
    #mousePress
    
    def mouseMove(self, brush, painter, oldPos, newPos):
        painter.save()
        rad = painter.pen().width() / 2
        
        boundingRect=QRect(oldPos, newPos).normalized().adjusted(-rad, -rad, +rad, +rad)
        color=painter.pen().color()
        thickness=painter.pen().width()
        transparentColor=QColor(color.red(), color.green(), color.blue(), 0)
        device=painter.device()
        
        self.imageList.append(device.copy(boundingRect))
        self.posList.append((oldPos, newPos))

        if brush=="Pencil":
            painter.drawLine(oldPos, newPos)
        elif brush=="Air Brush":
            numSteps = 2+(newPos-oldPos).manhattanLength() / 2
            
            painter.setBrush(QBrush(color, Qt.Dense6Pattern))
            painter.setPen(Qt.NoPen)
            
            for i in xrange(numSteps):
                x=oldPos.x()+i*(newPos.x()-oldPos.x())/(numSteps-1)
                y=oldPos.y()+i*(newPos.y()-oldPos.y())/(numSteps-1)
                
                painter.drawEllipse(x-(thickness/2), y-(thickness/2), thickness, thickness)
                
        elif brush=="Random Letters":
            char = charList[int(26*random())]
            
            biggerFont=painter.font()
            biggerFont.setBold(True)
            biggerFont.setPointSize(biggerFont.pointSize() + thickness )
            painter.setFont(biggerFont)
            
            painter.drawText(newPos, char)
            metrics=QFontMetrics(painter.font())
            boundingRect=metrics.boundingRect(char)
            boundingRect.translate(newPos)
            boundingRect.adjust(-10, -10, +10, +10)
            
        elif brush=="Rectangle":
            firstPos=self.posList[0][0]
            x=firstPos.x()
            y=firstPos.y()
            width=newPos.x()-x
            height=newPos.y()-y
            print x, y, width, height
            painter.drawRect(x, y, width, height)
            if width<0:
                x=x-width
                width=abs(width)
            if height<0:
                y=y-height
                height=abs(height)
                    
            boundingRect=QRect(x-2, y-2, width+4, height+4)
        painter.restore()
        return boundingRect
    #mouseMove
    
    def mouseRelease(self, brush, painter, pos):
        return QRect(0, 0, 0, 0)
    #mouseRelease
#BasicToolsPlugin
